package com.ryskoo.controller.customer;

import com.ryskoo.customer.vendorbasic.domain.VendorBasicPO;
import com.ryskoo.customer.vendorbasic.service.IVendorBasicService;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/vendor/vendorbasic")
public class VendorBasicController extends BasicHandler<VendorBasicPO> {
    private static final String TEMPLATE_PATH = "system/customer/vendorbasic";

    private static final String MODULE_NAME = "VendorBasic";

    /**
     * 基类构造方法.
     */
    public VendorBasicController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private IVendorBasicService vendorBasicService;

    /**
     * 消费者注入
     *
     * @param vendorBasicService 消费者基类
     */
    @Autowired
    protected void setService(IVendorBasicService vendorBasicService) {
        this.vendorBasicService = vendorBasicService;
        super.setService(vendorBasicService);
    }

    @GetMapping("/toSetIndex")
    public String toSetIndex(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorBasicPO vendorBasic = vendorBasicService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorBasic", vendorBasic);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetIndex";
    }

    /**
     * 商户首页设置 设置商户首页广告图 设置商户推荐分类
     *
     * @param vendorId
     * @param advert1
     * @param advert2
     * @param advert3
     * @param advert4
     * @param advert5
     * @param showSign
     * @param showPromotion
     * @param shopInvitingImg
     * @return
     */
    @PostMapping("/updateIndex")
    @ResponseBody
    public ReturnMessageDTO updateVendorName(@RequestParam(value = "vendorId") String vendorId,
                                             @RequestParam(value = "advert1") String advert1, @RequestParam(value = "advert2") String advert2,
                                             @RequestParam(value = "advert3") String advert3, @RequestParam(value = "advert4") String advert4,
                                             @RequestParam(value = "advert5") String advert5, @RequestParam(value = "showSign") Boolean showSign,
                                             @RequestParam(value = "showPromotion") Boolean showPromotion, @RequestParam(value = "shopInvitingImg") String shopInvitingImg) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        VendorBasicPO vendorBasic = vendorBasicService.getByVendorUuid(vendorId);
        vendorBasic.setAdvert1(advert1);
        vendorBasic.setAdvert2(advert2);
        vendorBasic.setAdvert3(advert3);
        vendorBasic.setAdvert4(advert4);
        vendorBasic.setAdvert5(advert5);
        vendorBasic.setShowSign(showSign);
        vendorBasic.setShowPromotion(showPromotion);
        vendorBasic.setShopInvitingImg(shopInvitingImg);
        vendorBasicService.update(vendorBasic);
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("保存首页设置成功");
        return returnMessage;
    }

    /**
     * 餐具费用设置
     *
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/tosetcover")
    public String toSetCover(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorBasicPO vendorBasic = vendorBasicService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorBasic", vendorBasic);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetCover";
    }

    @PostMapping("/setCover")
    @ResponseBody
    public ReturnMessageDTO setCover(@RequestBody VendorBasicPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorBasicService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }

    /**
     * 外卖设置设置
     *
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/tosetout")
    public String toSetOut(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorBasicPO vendorBasic = vendorBasicService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorBasic", vendorBasic);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetOut";
    }

    @PostMapping("/setOut")
    @ResponseBody
    public ReturnMessageDTO setOut(@RequestBody VendorBasicPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorBasicService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }

    /**
     * 分享设置
     *
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/toshareset")
    public String toShareSet(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorBasicPO vendorBasic = vendorBasicService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorBasic", vendorBasic);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetShare";
    }

    @PostMapping("/setShare")
    @ResponseBody
    public ReturnMessageDTO setShare(@RequestBody VendorBasicPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorBasicService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }
}
